﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
    [Serializable()]
    public class HoSoBO : Csla.BusinessBase<HoSoBO>
    {
        #region Business Properties

        private int? _HoSoID = null;
        public int? CL_HoSoID
        {
            get { return this._HoSoID; }
            set { this._HoSoID = value; PropertyHasChanged("CL_HoSoID"); }
        }

        private string _MaHoSo = null;
        public string CL_MaHoSo
        {
            get { return this._MaHoSo; }
            set { this._MaHoSo = value; PropertyHasChanged("CL_MaHoSo"); }
        }

        private DateTime? _NgayNopHoSo = DateTime.Now;
        public DateTime? CL_NgayNopHoSo
        {
            get { return this._NgayNopHoSo; }
            set { this._NgayNopHoSo = value; PropertyHasChanged("CL_NgayNopHoSo"); }
        }

        private bool? _LPTB = false;
        public bool? CL_LPTB
        {
            get { return this._LPTB; }
            set { this._LPTB = value; PropertyHasChanged("CL_LPTB"); }
        }

        private bool? _TSDD = false;
        public bool? CL_TSDD
        {
            get { return this._TSDD; }
            set { this._TSDD = value; PropertyHasChanged("CL_TSDD"); }
        }

        private bool? _BoSungLPTB = false;
        public bool? CL_BoSungLPTB
        {
            get { return this._BoSungLPTB; }
            set { this._BoSungLPTB = value; PropertyHasChanged("CL_BoSungLPTB"); }
        }

        private bool? _TNDN = false;
        public bool? CL_TNDN
        {
            get { return this._TNDN; }
            set { this._TNDN = value; PropertyHasChanged("CL_TNDN"); }
        }

        private bool? _TNCN = false;
        public bool? CL_TNCN
        {
            get { return this._TNCN; }
            set { this._TNCN = value; PropertyHasChanged("CL_TNCN"); }
        }

        private string _MaSoPhieuChuyen = null;
        public string CL_MaSoPhieuChuyen
        {
            get { return this._MaSoPhieuChuyen; }
            set { this._MaSoPhieuChuyen = value; PropertyHasChanged("CL_MaSoPhieuChuyen"); }
        }

        private DateTime? _NgayChuyen = null;
        public DateTime? CL_NgayChuyen
        {
            get { return this._NgayChuyen; }
            set { this._NgayChuyen = value; PropertyHasChanged("CL_NgayChuyen"); }
        }

        private string _ThuaDatSo = null;
        public string CL_ThuaDatSo
        {
            get { return this._ThuaDatSo; }
            set { this._ThuaDatSo = value; PropertyHasChanged("CL_ThuaDatSo"); }
        }

        private string _BanDoSo = null;
        public string CL_BanDoSo
        {
            get { return this._BanDoSo; }
            set { this._BanDoSo = value; PropertyHasChanged("CL_BanDoSo"); }
        }

        private int? _DiaPhuongID = null;
        public int? CL_DiaPhuongID
        {
            get { return this._DiaPhuongID; }
            set { this._DiaPhuongID = value; PropertyHasChanged("CL_DiaPhuongID"); }
        }

        private string _DiaPhuongID_Text = null;
        public string CL_DiaPhuongID_Text
        {
            get { return this._DiaPhuongID_Text; }
            set { this._DiaPhuongID_Text = value; PropertyHasChanged("CL_DiaPhuongID_Text"); }
        }

        private string _DiaChiDat = null;
        public string CL_DiaChiDat
        {
            get { return this._DiaChiDat; }
            set { this._DiaChiDat = value; PropertyHasChanged("CL_DiaChiDat"); }
        }

        private double? _DienTich = 0;
        public double? CL_DienTich
        {
            get { return this._DienTich; }
            set { this._DienTich = value; PropertyHasChanged("CL_DienTich"); }
        }

        private string _LoaiDuongKV = null;
        public string CL_LoaiDuongKV
        {
            get { return this._LoaiDuongKV; }
            set { this._LoaiDuongKV = value; PropertyHasChanged("CL_LoaiDuongKV"); }
        }

        private string _ViTri = null;
        public string CL_ViTri
        {
            get { return this._ViTri; }
            set { this._ViTri = value; PropertyHasChanged("CL_ViTri"); }
        }

        private string _LoaiDat = null;
        public string CL_LoaiDat
        {
            get { return this._LoaiDat; }
            set { this._LoaiDat = value; PropertyHasChanged("CL_LoaiDat"); }
        }

        private string _BenGiao = null;
        public string CL_BenGiao
        {
            get { return this._BenGiao; }
            set { this._BenGiao = value; PropertyHasChanged("CL_BenGiao"); }
        }

        private string _BenGiao_MST = null;
        public string CL_BenGiao_MST
        {
            get { return this._BenGiao_MST; }
            set { this._BenGiao_MST = value; PropertyHasChanged("CL_BenGiao_MST"); }
        }

        private string _BenGiao_DiaChi = null;
        public string CL_BenGiao_DiaChi
        {
            get { return this._BenGiao_DiaChi; }
            set { this._BenGiao_DiaChi = value; PropertyHasChanged("CL_BenGiao_DiaChi"); }
        }

        private string _BenNhan = null;
        public string CL_BenNhan
        {
            get { return this._BenNhan; }
            set { this._BenNhan = value; PropertyHasChanged("CL_BenNhan"); }
        }

        private string _BenNhan_MST = null;
        public string CL_BenNhan_MST
        {
            get { return this._BenNhan_MST; }
            set { this._BenNhan_MST = value; PropertyHasChanged("CL_BenNhan_MST"); }
        }

        private string _BenNhan_DiaChi = null;
        public string CL_BenNhan_DiaChi
        {
            get { return this._BenNhan_DiaChi; }
            set { this._BenNhan_DiaChi = value; PropertyHasChanged("CL_BenNhan_DiaChi"); }
        }

        private string _NguoiKeKhai = null;
        public string CL_NguoiKeKhai
        {
            get { return this._NguoiKeKhai; }
            set { this._NguoiKeKhai = value; PropertyHasChanged("CL_NguoiKeKhai"); }
        }

        private string _NguoiKeKhai_DiaChi = null;
        public string CL_NguoiKeKhai_DiaChi
        {
            get { return this._NguoiKeKhai_DiaChi; }
            set { this._NguoiKeKhai_DiaChi = value; PropertyHasChanged("CL_NguoiKeKhai_DiaChi"); }
        }

        private double? _TriGiaDat = 0;
        public double? CL_TriGiaDat
        {
            get { return this._TriGiaDat; }
            set { this._TriGiaDat = value; PropertyHasChanged("CL_TriGiaDat"); }
        }

        private double? _TriGiaNha = 0;
        public double? CL_TriGiaNha
        {
            get { return this._TriGiaNha; }
            set { this._TriGiaNha = value; PropertyHasChanged("CL_TriGiaNha"); }
        }

        private int? _NguonGocID = null;
        public int? CL_NguonGocID
        {
            get { return this._NguonGocID; }
            set { this._NguonGocID = value; PropertyHasChanged("CL_NguonGocID"); }
        }

        private string _NguonGocID_Text = null;
        public string CL_NguonGocID_Text
        {
            get { return this._NguonGocID_Text; }
            set { this._NguonGocID_Text = value; PropertyHasChanged("CL_NguonGocID_Text"); }
        }

        private bool? _MucNop_100TSDD = false;
        public bool? CL_MucNop_100TSDD
        {
            get { return this._MucNop_100TSDD; }
            set { this._MucNop_100TSDD = value; PropertyHasChanged("CL_MucNop_100TSDD"); }
        }

        private bool? _MucNop_50TSDD = false;
        public bool? CL_MucNop_50TSDD
        {
            get { return this._MucNop_50TSDD; }
            set { this._MucNop_50TSDD = value; PropertyHasChanged("CL_MucNop_50TSDD"); }
        }

        private bool? _MucNop_CL2GiaDat = false;
        public bool? CL_MucNop_CL2GiaDat
        {
            get { return this._MucNop_CL2GiaDat; }
            set { this._MucNop_CL2GiaDat = value; PropertyHasChanged("CL_MucNop_CL2GiaDat"); }
        }

        private bool? _MucNop_50CL2GiaDat = false;
        public bool? CL_MucNop_50CL2GiaDat
        {
            get { return this._MucNop_50CL2GiaDat; }
            set { this._MucNop_50CL2GiaDat = value; PropertyHasChanged("CL_MucNop_50CL2GiaDat"); }
        }

        private bool? _MucNop_50GiaDat = false;
        public bool? CL_MucNop_50GiaDat
        {
            get { return this._MucNop_50GiaDat; }
            set { this._MucNop_50GiaDat = value; PropertyHasChanged("CL_MucNop_50GiaDat"); }
        }

        private bool? _MucNop_Khac = false;
        public bool? CL_MucNop_Khac
        {
            get { return this._MucNop_Khac; }
            set { this._MucNop_Khac = value; PropertyHasChanged("CL_MucNop_Khac"); }
        }

        private string _VanBanSDDIDList = null;
        public string CL_VanBanSDDIDList
        {
            get { return this._VanBanSDDIDList; }
            set { this._VanBanSDDIDList = value; PropertyHasChanged("CL_VanBanSDDIDList"); }
        }

        private string _VanBanSDDIDList_Text = null;
        public string CL_VanBanSDDIDList_Text
        {
            get { return this._VanBanSDDIDList_Text; }
            set { this._VanBanSDDIDList_Text = value; PropertyHasChanged("CL_VanBanSDDIDList_Text"); }
        }

        private DateTime? _NgayQD = null;
        public DateTime? CL_NgayQD
        {
            get { return this._NgayQD; }
            set { this._NgayQD = value; PropertyHasChanged("CL_NgayQD"); }
        }

        private string _SoQDGiaoDat = null;
        public string CL_SoQDGiaoDat
        {
            get { return this._SoQDGiaoDat; }
            set { this._SoQDGiaoDat = value; PropertyHasChanged("CL_SoQDGiaoDat"); }
        }

        private string _CoQuanQD = null;
        public string CL_CoQuanQD
        {
            get { return this._CoQuanQD; }
            set { this._CoQuanQD = value; PropertyHasChanged("CL_CoQuanQD"); }
        }

        private string _GhiChu = null;
        public string CL_GhiChu
        {
            get { return this._GhiChu; }
            set { this._GhiChu = value; PropertyHasChanged("CL_GhiChu"); }
        }

        private int? _USRC = Session.LoginID;
        public int? CL_USRC
        {
            get { return this._USRC; }
            set { this._USRC = value; PropertyHasChanged("CL_USRC"); }
        }

        private DateTime? _DATC = DateTime.MaxValue;
        public DateTime? CL_DATC
        {
            get { return this._DATC; }
            set { this._DATC = value; PropertyHasChanged("CL_DATC"); }
        }

        private int? _USRM = null;
        public int? CL_USRM
        {
            get { return this._USRM; }
            set { this._USRM = value; PropertyHasChanged("CL_USRM"); }
        }

        private DateTime? _DATM = null;
        public DateTime? CL_DATM
        {
            get { return this._DATM; }
            set { this._DATM = value; PropertyHasChanged("CL_DATM"); }
        }

        private bool? _DELE = false;
        public bool? CL_DELE
        {
            get { return this._DELE; }
            set { this._DELE = value; PropertyHasChanged("CL_DELE"); }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; PropertyHasChanged("STT0"); }
        }

        protected override object GetIdValue()
        {
            return _HoSoID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newHoSoID = -1;
        private HoSoBO()
        {
            _HoSoID = newHoSoID--;
        }

        public HoSoBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public HoSoBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static HoSoBO NewRootHoSoBO()
        {
            return DataPortal.Create<HoSoBO>(new RootCriteria());
        }

        internal static HoSoBO NewChildHoSoBO()
        {
            return DataPortal.Create<HoSoBO>(new ChildCriteria());
        }

        public static HoSoBO GetRootHoSoBO(int _HoSoID)
        {
            return DataPortal.Fetch<HoSoBO>(new RootCriteria(_HoSoID));
        }

        public static HoSoBO GetRootHoSoBOByWhere(string where)
        {
            return DataPortal.Fetch<HoSoBO>(where);
        }

        internal static HoSoBO GetChildHoSoBO(SqlDataReader dataReader)
        {
            return new HoSoBO(dataReader);
        }

        public static void DeleteHoSoBO(int _HoSoID)
        {
            DataPortal.Delete(new RootCriteria(_HoSoID));
        }

        public static void DeleteHoSoBO(int _HoSoID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_HoSoID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int HoSoID;
            public bool directDelete = false;
            public RootCriteria(int _HoSoID)
            {
                this.HoSoID = _HoSoID;
                this.directDelete = false;
            }

            public RootCriteria(int _HoSoID, bool directDelete)
            {
                this.HoSoID = _HoSoID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int HoSoID;
            public bool directDelete = false;
            public ChildCriteria(int _HoSoID)
            {
                this.HoSoID = _HoSoID;
                this.directDelete = false;
            }

            public ChildCriteria(int _HoSoID, bool directDelete)
            {
                this.HoSoID = _HoSoID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("HoSoBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[HoSoID] = " + criteria.HoSoID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("HoSoBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["HoSoID"] is DBNull))
                this._HoSoID = (int)dataReader["HoSoID"];
            if (!(dataReader["MaHoSo"] is DBNull))
                this._MaHoSo = (string)dataReader["MaHoSo"];
            if (!(dataReader["NgayNopHoSo"] is DBNull))
                this._NgayNopHoSo = (DateTime)dataReader["NgayNopHoSo"];
            if (!(dataReader["LPTB"] is DBNull))
                this._LPTB = (bool)dataReader["LPTB"];
            if (!(dataReader["TSDD"] is DBNull))
                this._TSDD = (bool)dataReader["TSDD"];
            if (!(dataReader["BoSungLPTB"] is DBNull))
                this._BoSungLPTB = (bool)dataReader["BoSungLPTB"];
            if (!(dataReader["TNDN"] is DBNull))
                this._TNDN = (bool)dataReader["TNDN"];
            if (!(dataReader["TNCN"] is DBNull))
                this._TNCN = (bool)dataReader["TNCN"];
            if (!(dataReader["MaSoPhieuChuyen"] is DBNull))
                this._MaSoPhieuChuyen = (string)dataReader["MaSoPhieuChuyen"];
            if (!(dataReader["NgayChuyen"] is DBNull))
                this._NgayChuyen = (DateTime)dataReader["NgayChuyen"];
            if (!(dataReader["ThuaDatSo"] is DBNull))
                this._ThuaDatSo = (string)dataReader["ThuaDatSo"];
            if (!(dataReader["BanDoSo"] is DBNull))
                this._BanDoSo = (string)dataReader["BanDoSo"];
            if (!(dataReader["DiaPhuongID"] is DBNull))
                this._DiaPhuongID = (int)dataReader["DiaPhuongID"];
            if (!(dataReader["DiaPhuongID_Text"] is DBNull))
                this._DiaPhuongID_Text = (string)dataReader["DiaPhuongID_Text"];
            if (!(dataReader["DiaChiDat"] is DBNull))
                this._DiaChiDat = (string)dataReader["DiaChiDat"];
            if (!(dataReader["DienTich"] is DBNull))
                this._DienTich = (double)dataReader["DienTich"];
            if (!(dataReader["LoaiDuongKV"] is DBNull))
                this._LoaiDuongKV = (string)dataReader["LoaiDuongKV"];
            if (!(dataReader["ViTri"] is DBNull))
                this._ViTri = (string)dataReader["ViTri"];
            if (!(dataReader["LoaiDat"] is DBNull))
                this._LoaiDat = (string)dataReader["LoaiDat"];
            if (!(dataReader["BenGiao"] is DBNull))
                this._BenGiao = (string)dataReader["BenGiao"];
            if (!(dataReader["BenGiao_MST"] is DBNull))
                this._BenGiao_MST = (string)dataReader["BenGiao_MST"];
            if (!(dataReader["BenGiao_DiaChi"] is DBNull))
                this._BenGiao_DiaChi = (string)dataReader["BenGiao_DiaChi"];
            if (!(dataReader["BenNhan"] is DBNull))
                this._BenNhan = (string)dataReader["BenNhan"];
            if (!(dataReader["BenNhan_MST"] is DBNull))
                this._BenNhan_MST = (string)dataReader["BenNhan_MST"];
            if (!(dataReader["BenNhan_DiaChi"] is DBNull))
                this._BenNhan_DiaChi = (string)dataReader["BenNhan_DiaChi"];
            if (!(dataReader["NguoiKeKhai"] is DBNull))
                this._NguoiKeKhai = (string)dataReader["NguoiKeKhai"];
            if (!(dataReader["NguoiKeKhai_DiaChi"] is DBNull))
                this._NguoiKeKhai_DiaChi = (string)dataReader["NguoiKeKhai_DiaChi"];
            if (!(dataReader["TriGiaDat"] is DBNull))
                this._TriGiaDat = (double)dataReader["TriGiaDat"];
            if (!(dataReader["TriGiaNha"] is DBNull))
                this._TriGiaNha = (double)dataReader["TriGiaNha"];
            if (!(dataReader["NguonGocID"] is DBNull))
                this._NguonGocID = (int)dataReader["NguonGocID"];
            if (!(dataReader["NguonGocID_Text"] is DBNull))
                this._NguonGocID_Text = (string)dataReader["NguonGocID_Text"];
            if (!(dataReader["MucNop_100TSDD"] is DBNull))
                this._MucNop_100TSDD = (bool)dataReader["MucNop_100TSDD"];
            if (!(dataReader["MucNop_50TSDD"] is DBNull))
                this._MucNop_50TSDD = (bool)dataReader["MucNop_50TSDD"];
            if (!(dataReader["MucNop_CL2GiaDat"] is DBNull))
                this._MucNop_CL2GiaDat = (bool)dataReader["MucNop_CL2GiaDat"];
            if (!(dataReader["MucNop_50CL2GiaDat"] is DBNull))
                this._MucNop_50CL2GiaDat = (bool)dataReader["MucNop_50CL2GiaDat"];
            if (!(dataReader["MucNop_50GiaDat"] is DBNull))
                this._MucNop_50GiaDat = (bool)dataReader["MucNop_50GiaDat"];
            if (!(dataReader["MucNop_Khac"] is DBNull))
                this._MucNop_Khac = (bool)dataReader["MucNop_Khac"];
            if (!(dataReader["VanBanSDDIDList"] is DBNull))
                this._VanBanSDDIDList = (string)dataReader["VanBanSDDIDList"];
            if (!(dataReader["VanBanSDDIDList_Text"] is DBNull))
                this._VanBanSDDIDList_Text = (string)dataReader["VanBanSDDIDList_Text"];
            if (!(dataReader["NgayQD"] is DBNull))
                this._NgayQD = (DateTime)dataReader["NgayQD"];
            if (!(dataReader["SoQDGiaoDat"] is DBNull))
                this._SoQDGiaoDat = (string)dataReader["SoQDGiaoDat"];
            if (!(dataReader["CoQuanQD"] is DBNull))
                this._CoQuanQD = (string)dataReader["CoQuanQD"];
            if (!(dataReader["GhiChu"] is DBNull))
                this._GhiChu = (string)dataReader["GhiChu"];
            if (!(dataReader["USRC"] is DBNull))
                this._USRC = (int)dataReader["USRC"];
            if (!(dataReader["DATC"] is DBNull))
                this._DATC = (DateTime)dataReader["DATC"];
            if (!(dataReader["USRM"] is DBNull))
                this._USRM = (int)dataReader["USRM"];
            if (!(dataReader["DATM"] is DBNull))
                this._DATM = (DateTime)dataReader["DATM"];
            if (!(dataReader["DELE"] is DBNull))
                this._DELE = (bool)dataReader["DELE"];

        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
            this._USRC = Session.LoginID;
            this._DATC = DateTime.MaxValue;
            this._USRM = null;
            this._DATM = null;
            this._DELE = false;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("HoSoBO_InsertBO");
            db.AddInParameter(cmd, "@MaHoSo", SqlDbType.NVarChar, this._MaHoSo);
            db.AddInParameter(cmd, "@NgayNopHoSo", SqlDbType.DateTime, this._NgayNopHoSo);
            db.AddInParameter(cmd, "@LPTB", SqlDbType.Bit, this._LPTB);
            db.AddInParameter(cmd, "@TSDD", SqlDbType.Bit, this._TSDD);
            db.AddInParameter(cmd, "@BoSungLPTB", SqlDbType.Bit, this._BoSungLPTB);
            db.AddInParameter(cmd, "@TNDN", SqlDbType.Bit, this._TNDN);
            db.AddInParameter(cmd, "@TNCN", SqlDbType.Bit, this._TNCN);
            db.AddInParameter(cmd, "@MaSoPhieuChuyen", SqlDbType.NVarChar, this._MaSoPhieuChuyen);
            db.AddInParameter(cmd, "@NgayChuyen", SqlDbType.DateTime, this._NgayChuyen);
            db.AddInParameter(cmd, "@ThuaDatSo", SqlDbType.NVarChar, this._ThuaDatSo);
            db.AddInParameter(cmd, "@BanDoSo", SqlDbType.NVarChar, this._BanDoSo);
            db.AddInParameter(cmd, "@DiaPhuongID", SqlDbType.Int, this._DiaPhuongID);
            db.AddInParameter(cmd, "@DiaPhuongID_Text", SqlDbType.NVarChar, this._DiaPhuongID_Text);
            db.AddInParameter(cmd, "@DiaChiDat", SqlDbType.NVarChar, this._DiaChiDat);
            db.AddInParameter(cmd, "@DienTich", SqlDbType.Float, this._DienTich);
            db.AddInParameter(cmd, "@LoaiDuongKV", SqlDbType.NVarChar, this._LoaiDuongKV);
            db.AddInParameter(cmd, "@ViTri", SqlDbType.NVarChar, this._ViTri);
            db.AddInParameter(cmd, "@LoaiDat", SqlDbType.NVarChar, this._LoaiDat);
            db.AddInParameter(cmd, "@BenGiao", SqlDbType.NVarChar, this._BenGiao);
            db.AddInParameter(cmd, "@BenGiao_MST", SqlDbType.NVarChar, this._BenGiao_MST);
            db.AddInParameter(cmd, "@BenGiao_DiaChi", SqlDbType.NVarChar, this._BenGiao_DiaChi);
            db.AddInParameter(cmd, "@BenNhan", SqlDbType.NVarChar, this._BenNhan);
            db.AddInParameter(cmd, "@BenNhan_MST", SqlDbType.NVarChar, this._BenNhan_MST);
            db.AddInParameter(cmd, "@BenNhan_DiaChi", SqlDbType.NVarChar, this._BenNhan_DiaChi);
            db.AddInParameter(cmd, "@NguoiKeKhai", SqlDbType.NVarChar, this._NguoiKeKhai);
            db.AddInParameter(cmd, "@NguoiKeKhai_DiaChi", SqlDbType.NVarChar, this._NguoiKeKhai_DiaChi);
            db.AddInParameter(cmd, "@TriGiaDat", SqlDbType.Float, this._TriGiaDat);
            db.AddInParameter(cmd, "@TriGiaNha", SqlDbType.Float, this._TriGiaNha);
            db.AddInParameter(cmd, "@NguonGocID", SqlDbType.Int, this._NguonGocID);
            db.AddInParameter(cmd, "@NguonGocID_Text", SqlDbType.NVarChar, this._NguonGocID_Text);
            db.AddInParameter(cmd, "@MucNop_100TSDD", SqlDbType.Bit, this._MucNop_100TSDD);
            db.AddInParameter(cmd, "@MucNop_50TSDD", SqlDbType.Bit, this._MucNop_50TSDD);
            db.AddInParameter(cmd, "@MucNop_CL2GiaDat", SqlDbType.Bit, this._MucNop_CL2GiaDat);
            db.AddInParameter(cmd, "@MucNop_50CL2GiaDat", SqlDbType.Bit, this._MucNop_50CL2GiaDat);
            db.AddInParameter(cmd, "@MucNop_50GiaDat", SqlDbType.Bit, this._MucNop_50GiaDat);
            db.AddInParameter(cmd, "@MucNop_Khac", SqlDbType.Bit, this._MucNop_Khac);
            db.AddInParameter(cmd, "@VanBanSDDIDList", SqlDbType.NVarChar, this._VanBanSDDIDList);
            db.AddInParameter(cmd, "@VanBanSDDIDList_Text", SqlDbType.NVarChar, this._VanBanSDDIDList_Text);
            db.AddInParameter(cmd, "@NgayQD", SqlDbType.DateTime, this._NgayQD);
            db.AddInParameter(cmd, "@SoQDGiaoDat", SqlDbType.NVarChar, this._SoQDGiaoDat);
            db.AddInParameter(cmd, "@CoQuanQD", SqlDbType.NVarChar, this._CoQuanQD);
            db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
            db.AddInParameter(cmd, "@USRC", SqlDbType.Int, this._USRC);
            db.AddOutParameter(cmd, "@NewHoSoID", SqlDbType.Int, 10);
            db.AddOutParameter(cmd, "@NewDATC", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);
            this._HoSoID = (int)cmd.Parameters["@NewHoSoID"].Value;
            this._DATC = (DateTime)cmd.Parameters["@NewDATC"].Value;
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
            this._USRM = Session.LoginID;
            this._DATM = DateTime.Now;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("HoSoBO_UpdateBO");
            db.AddInParameter(cmd, "@HoSoID", SqlDbType.Int, this._HoSoID);
            db.AddInParameter(cmd, "@MaHoSo", SqlDbType.NVarChar, this._MaHoSo);
            db.AddInParameter(cmd, "@NgayNopHoSo", SqlDbType.DateTime, this._NgayNopHoSo);
            db.AddInParameter(cmd, "@LPTB", SqlDbType.Bit, this._LPTB);
            db.AddInParameter(cmd, "@TSDD", SqlDbType.Bit, this._TSDD);
            db.AddInParameter(cmd, "@BoSungLPTB", SqlDbType.Bit, this._BoSungLPTB);
            db.AddInParameter(cmd, "@TNDN", SqlDbType.Bit, this._TNDN);
            db.AddInParameter(cmd, "@TNCN", SqlDbType.Bit, this._TNCN);
            db.AddInParameter(cmd, "@MaSoPhieuChuyen", SqlDbType.NVarChar, this._MaSoPhieuChuyen);
            db.AddInParameter(cmd, "@NgayChuyen", SqlDbType.DateTime, this._NgayChuyen);
            db.AddInParameter(cmd, "@ThuaDatSo", SqlDbType.NVarChar, this._ThuaDatSo);
            db.AddInParameter(cmd, "@BanDoSo", SqlDbType.NVarChar, this._BanDoSo);
            db.AddInParameter(cmd, "@DiaPhuongID", SqlDbType.Int, this._DiaPhuongID);
            db.AddInParameter(cmd, "@DiaPhuongID_Text", SqlDbType.NVarChar, this._DiaPhuongID_Text);
            db.AddInParameter(cmd, "@DiaChiDat", SqlDbType.NVarChar, this._DiaChiDat);
            db.AddInParameter(cmd, "@DienTich", SqlDbType.Float, this._DienTich);
            db.AddInParameter(cmd, "@LoaiDuongKV", SqlDbType.NVarChar, this._LoaiDuongKV);
            db.AddInParameter(cmd, "@ViTri", SqlDbType.NVarChar, this._ViTri);
            db.AddInParameter(cmd, "@LoaiDat", SqlDbType.NVarChar, this._LoaiDat);
            db.AddInParameter(cmd, "@BenGiao", SqlDbType.NVarChar, this._BenGiao);
            db.AddInParameter(cmd, "@BenGiao_MST", SqlDbType.NVarChar, this._BenGiao_MST);
            db.AddInParameter(cmd, "@BenGiao_DiaChi", SqlDbType.NVarChar, this._BenGiao_DiaChi);
            db.AddInParameter(cmd, "@BenNhan", SqlDbType.NVarChar, this._BenNhan);
            db.AddInParameter(cmd, "@BenNhan_MST", SqlDbType.NVarChar, this._BenNhan_MST);
            db.AddInParameter(cmd, "@BenNhan_DiaChi", SqlDbType.NVarChar, this._BenNhan_DiaChi);
            db.AddInParameter(cmd, "@NguoiKeKhai", SqlDbType.NVarChar, this._NguoiKeKhai);
            db.AddInParameter(cmd, "@NguoiKeKhai_DiaChi", SqlDbType.NVarChar, this._NguoiKeKhai_DiaChi);
            db.AddInParameter(cmd, "@TriGiaDat", SqlDbType.Float, this._TriGiaDat);
            db.AddInParameter(cmd, "@TriGiaNha", SqlDbType.Float, this._TriGiaNha);
            db.AddInParameter(cmd, "@NguonGocID", SqlDbType.Int, this._NguonGocID);
            db.AddInParameter(cmd, "@NguonGocID_Text", SqlDbType.NVarChar, this._NguonGocID_Text);
            db.AddInParameter(cmd, "@MucNop_100TSDD", SqlDbType.Bit, this._MucNop_100TSDD);
            db.AddInParameter(cmd, "@MucNop_50TSDD", SqlDbType.Bit, this._MucNop_50TSDD);
            db.AddInParameter(cmd, "@MucNop_CL2GiaDat", SqlDbType.Bit, this._MucNop_CL2GiaDat);
            db.AddInParameter(cmd, "@MucNop_50CL2GiaDat", SqlDbType.Bit, this._MucNop_50CL2GiaDat);
            db.AddInParameter(cmd, "@MucNop_50GiaDat", SqlDbType.Bit, this._MucNop_50GiaDat);
            db.AddInParameter(cmd, "@MucNop_Khac", SqlDbType.Bit, this._MucNop_Khac);
            db.AddInParameter(cmd, "@VanBanSDDIDList", SqlDbType.NVarChar, this._VanBanSDDIDList);
            db.AddInParameter(cmd, "@VanBanSDDIDList_Text", SqlDbType.NVarChar, this._VanBanSDDIDList_Text);
            db.AddInParameter(cmd, "@NgayQD", SqlDbType.DateTime, this._NgayQD);
            db.AddInParameter(cmd, "@SoQDGiaoDat", SqlDbType.NVarChar, this._SoQDGiaoDat);
            db.AddInParameter(cmd, "@CoQuanQD", SqlDbType.NVarChar, this._CoQuanQD);
            db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
            db.AddInParameter(cmd, "@USRM", SqlDbType.Int, this._USRM);
            db.AddInParameter(cmd, "@DELE", SqlDbType.Bit, this._DELE);

            db.AddOutParameter(cmd, "@NewDATM", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);

            this._DATM = (DateTime)cmd.Parameters["@NewDATM"].Value;
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_HoSoID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("HoSoBO_DeleteBO");
            db.AddInParameter(cmd, "@DirectDelete", SqlDbType.Bit, criteria.directDelete);
            db.AddInParameter(cmd, "@HoSoID", SqlDbType.Int, criteria.HoSoID);
            db.ExecuteNonQuery(cmd);
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_HoSoID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
